package com.sdcqjy.mylibrary.http;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.AndroidConnectionSource;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.sdcqjy.mylibrary.Logger;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by 52463 on 2018/3/23.
 */

public class DownloadDB extends OrmLiteSqliteOpenHelper {
    public DownloadDB(Context context) {
        super(context, "BuLaDownloadDB", null, 1);
    }

    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {    //创建表
        try {
            TableUtils.createTable(connectionSource, DownloadMode.class);
        } catch (SQLException e) {
            if (Logger.isDebug) e.printStackTrace();
        }
    }

    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    }

    public List<DownloadMode> getTaskAll() {
        AndroidConnectionSource connection = new AndroidConnectionSource(this);
        try {
            Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
            List<DownloadMode> modeList = dao.queryBuilder().query();
            return modeList;
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        return null;
    }

    /**
     * 从名称获取下载信息
     */
    public List<DownloadMode> getTaskOfName(String name) {
        AndroidConnectionSource connection = new AndroidConnectionSource(this);
        try {
            Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
            List<DownloadMode> modeList = dao.queryBuilder().where().eq("name", name).query();
            return modeList;
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        return null;
    }

    /**
     * 插入一条记录
     */
    public void insertTask(DownloadMode mode) {
        AndroidConnectionSource connection = new AndroidConnectionSource(this);
        try {
            Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
            dao.create(mode);
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        showTaskAll();
    }

    /**
     * 更新一条数据，仅更新总长度和当前长度
     */
    public void updateTask(DownloadMode mode) {
        AndroidConnectionSource connection = new AndroidConnectionSource(this);
        try {
            Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
            dao.updateRaw("update downloadtable set currLen=? , total=? where name=?",
                    "" + mode.currLen, "" + mode.total, mode.name);
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        showTaskAll();
    }

    public void deleteTaskAll() {
        if (Logger.isDebug) {
            AndroidConnectionSource connection = new AndroidConnectionSource(this);
            try {
                Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
                dao.executeRaw("delete from downloadtable");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除一条记录
     */
    public void deleteTask(String name) {
        AndroidConnectionSource connection = new AndroidConnectionSource(this);
        try {
            Dao<DownloadMode, ?> dao = DaoManager.createDao(connection, DownloadMode.class);
            dao.executeRaw("delete from downloadtable where name=?", name);
        } catch (Exception e) {
            if (Logger.isDebug) e.printStackTrace();
        }
        showTaskAll();
    }

    //数据展示
    public void showTaskAll() {
//        if (Logger.isDebug) {
//            List<DownloadMode> taskAll = getTaskAll();
//            Logger.v("----当前数据----", new Gson().toJson(taskAll));
//        }
    }

    //清除没有用的数据
    public void clearData() {
        List<DownloadMode> taskAll = getTaskAll();
        for (int i = 0; i < taskAll.size(); i++) {
            DownloadMode mode = taskAll.get(i);
            File file = new File(mode.path);
            if (!file.exists()) {
                deleteTask(mode.name);
            }
        }
    }
}
